package com.bjsxt.service.impl;

import com.bjsxt.mapper.AdminMapper;
import com.bjsxt.pojo.Admin;
import com.bjsxt.pojo.PageA;
import com.bjsxt.pojo.Role;
import com.bjsxt.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
  @Autowired
   private AdminMapper adminMapper;
  //用户的查询
    @Override
    public Admin login(String aname, String apwd) {
        return adminMapper.selectOne(aname, apwd);
    }
   /*查询全部的用户信息*/
    @Override
    public PageA<Admin> findAllAdmin(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);
        List<Admin> list = adminMapper.select();
        PageA<Admin> pga=new PageA<>(list,pg.getTotal());
        return pga;
    }
     //添加用户
    @Override
    public Integer save(Admin admin,Integer rid) {
        int a=0;
        try {
            //添加用户
            adminMapper.insert(admin);
            //添加用户对应的角色
            adminMapper.insertRole(admin.getAid(),rid);
            a=1;
        } catch (Exception e) {
            throw  e;
        }
        return a;
    }
    //删除用户
    @Override
    public Integer detele(Integer aid) {
        int a=0;
        try {
            //删除指定的用户
            adminMapper.delete(aid);
            //删除用户对应的角色
            adminMapper.deleteRole(aid);
            a=1;
        } catch (Exception e) {
           throw e;
        }
        return a;
    }

    //查询指定用户对用的角色
    @Override
    public Integer findOneRole(Integer aid) {
        return adminMapper.findOne(aid);
    }
   //修改指定用户的信息
    @Override
    public Integer update(Admin admin,Integer rid) {
        int a=0;
        try {
            adminMapper.update(admin);
            adminMapper.updateRole(admin.getAid(),rid);
            a=1;
        } catch (Exception e) {
            throw  e;
        }

        return a;
    }
}
